package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNReward;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.widget.CenterAlignImageSpan;

import java.util.List;

public class MembersHorizontalTopAdapter extends RecyclerView.Adapter<MembersHorizontalTopAdapter.ViewHolder> {
    private Context mContext;
    private List<YNReward> datas;

    public MembersHorizontalTopAdapter(Context mContext, List<YNReward> datas) {
        this.mContext = mContext;
        this.datas = datas;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView iv_item_album_member_avatar;
        TextView iv_item_album_member_tip;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_item_album_member_avatar = (SimpleDraweeView) itemView
                    .findViewById(R.id.iv_item_album_member_avatar);
            iv_item_album_member_tip = (TextView) itemView
                    .findViewById(R.id.iv_item_album_member_tip);
        }

        public void bindData(final YNReward listItems) {
            if (listItems != null) {
                Uri avatarUri = Uri.parse(listItems.getUpic() + StringUtils.getQulity());
                ImageOptHelper.showFrescoAvatar(avatarUri, iv_item_album_member_avatar, mContext);
                String tx = listItems.getUnickname() + ": " + "收集了" + listItems.getRewardType() + "个赞，" + "获得" + listItems.getRewardContent();
                Spannable WordtoSpan = new SpannableString(tx);
                WordtoSpan.setSpan(new TextAppearanceSpan(mContext, R.style.item_huodong_text_style1), 0, tx.length() - listItems.getRewardContent().length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                WordtoSpan.setSpan(new TextAppearanceSpan(mContext, R.style.item_huodong_text_style2), tx.length() - listItems.getRewardContent().length(), tx.length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                Drawable drawable = mContext.getResources().getDrawable(R.drawable.tip_zan);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                WordtoSpan.setSpan(new CenterAlignImageSpan(drawable), tx.indexOf("赞"), tx.indexOf("赞") + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                iv_item_album_member_tip.setText(WordtoSpan);
            }
        }
    }


    // 创建ViewHolder
    @Override
    public MembersHorizontalTopAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_horizontal_membertop, parent, false);
        return new MembersHorizontalTopAdapter.ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(MembersHorizontalTopAdapter.ViewHolder holder, int position) {
        YNReward listItems = datas.get(position % datas.size());
        holder.bindData(listItems);
    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }

}
